layui.use(['table', 'admin', 'form', 'ax','layer'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var layer = layui.layer;

    // 合并行
    function merge(res) {
        var trArr = $(".layui-table-body>.layui-table").find("tr");
        for(var i = 0; i < trArr.length; i++) {
            if(i == trArr.length - 1) {
                var td = trArr.eq(i).find("td");
                td.eq(0).attr('colspan', 0);
                td.eq(0).css('height', '43px');
                td.eq(0).find('div').removeClass('laytable-cell-1-0-0');
                for (var j = 1; j < 8; j++) {
                    // td.eq(j).css("display", "none");
                }
            }
        }

    }


    var phTable = {
        tableId: "phTable"
    };

    var queryData = {};
    queryData['shopCode'] = Feng.getUrlParam("shopCode");
    queryData['shopName'] = '';
    queryData['beginTime'] = Feng.getUrlParam("beginTime");


    phTable.initColumn = function () {
        return  [[
            {field: 'num', width: 100, title: '', align: "center"},
            {field: 'shopCode', width: 100, title: '门店编号', align: "center"},
            {field: 'shopName', width: 200, title: '门店名称', templet:'#shopName',align: "center"},
            {field: 'className', width: 130, title: '品相类别名称', align: "center",},
            {field: 'proCode', width: 100, title: '品相编号', align: "center"},
            {field: 'proName', width: 120, title: '品相名称', align: "center",},
            {field: 'spec', width: 100, title: '规格', align: "center"},
            {field: 'unit', width: 80, title: '单位', align: "center",},
            {field: 'price', width: 100, title: '非税单价', align: "center"},
            {field: 'thNum', width: 100, title: '理论耗用数量', align: "center",},
            {field: 'realNum', width: 100, title: '实际耗用数量', align: "center",},
            {field: 'numTotal', width: 100, title: '耗用数量分差', align: "center"},
            {field: 'amountTotal', width: 100, title: '耗用金额分差', align: "center"},
            {field: 'soup', width: 90, title: '加汤等', align: "center",edit:'text'},
            {field: 'soupAmount', width: 95, title: '金额', align: "center",edit:'text'},
        ]]
    };
    var tableResult= table.render({
        elem: '#phTable',
        url: Feng.ctxPath + '/huopei/huopeiInfo',
        where: queryData,
        page: false,
        cellMinWidth: 150,//全局定义常规单元格的最小宽度，layui 2.2.1 新增
        cols:phTable.initColumn(),
        done:function (res, curr, count,) {
            // queryData['shopName'] = ;
            merge(res, curr, count);
            var that = this.elem.next();
            res.data.forEach(function (item, index) {
                if (item.shopName === "餐具盘亏") {
                    var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']");
                    // var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='className']");
                    var td1 = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='shopCode']");
                    tr.css("background-color", "#e1e2e6");
                    tr.css("color", "#666666");
                    // td.attr("data-edit", "text");
                    td1.css("color", "#e1e2e6");
                }
                if (item.shopName === "清江鱼盘亏") {
                    var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']");
                    // var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='className']");
                    var td1 = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='shopCode']");
                    tr.css("background-color", "#e1e2e6");
                    tr.css("color", "#666666");
                    // td.attr("data-edit", "text");
                    td1.css("color", "#e1e2e6");
                }
                if (item.shopName === "酱料盘亏") {
                    var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']");
                    // var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='className']");
                    var td1 = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='shopCode']");
                    tr.css("background-color", "#e1e2e6");
                    tr.css("color", "#666666");
                    // td.attr("data-edit", "text");
                    td1.css("color", "#e1e2e6");
                }
                if (item.shopName === "酒水盘亏") {
                    var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']");
                    // var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='className']");
                    var td1 = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='shopCode']");
                    tr.css("background-color", "#e1e2e6");
                    tr.css("color", "#666666");
                    // td.attr("data-edit", "text");
                    td1.css("color", "#e1e2e6");
                }
                if (item.shopName === "平板电脑应有库存") {
                    var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']");
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='className']");
                    var td1 = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='shopCode']");
                    tr.css("background-color", "#e1e2e6");
                    tr.css("color", "#666666");
                    td.attr("data-edit", "text");
                    td1.css("color", "#e1e2e6");
                }
                if (item.price === "平板货赔") {
                    var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']");
                    tr.css("background-color", "#e1e2e6");
                    tr.css("color", "#666666");
                }
                // if (item.proCode === "免赔") {
                //     var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='proName']");
                //     td.attr("data-edit", "text");
                // }
                if (item.proCode === "平板电脑盘点") {
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='proName']");
                    td.attr("data-edit", "text");
                }
                if (item.spec === "其他免赔(加汤等)") {
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='unit']");
                    td.attr("data-edit", "text");
                }
                if (item.price === "餐具货赔") {
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='thNum']");
                    td.attr("data-edit", "text");
                }
                if (item.price === "清江鱼货赔") {
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='thNum']");
                    td.attr("data-edit", "text");
                }
                if (item.price === "酱料货赔") {
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='thNum']");
                    td.attr("data-edit", "text");
                }
                if (item.price === "酒水货赔") {
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='thNum']");
                    td.attr("data-edit", "text");
                }
                if (item.price === "平板货赔") {
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='thNum']");
                    td.attr("data-edit", "text");
                }
                if (item.num === "小计") {
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='numTotal']").css("background-color", "#a6d792");
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='amountTotal']").css("background-color", "#a6d792");
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='soup']").removeAttr("data-edit", "text");
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='soupAmount']").removeAttr("data-edit", "text");
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='thNum']").removeAttr("data-edit", "text");
                }
                if (item.num === "总计") {
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='soup']").removeAttr("data-edit", "text");
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='soupAmount']").removeAttr("data-edit", "text");
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='thNum']").removeAttr("data-edit", "text");
                }
            });
            // 1 会计
            if($("#status").val()!="1"){
                $('#btnAudit').hide();
                $('#btnNotice').hide();
                $('input').attr('readonly','true');
                $('input').css('border-color','white');
                $("td").removeAttr("data-edit", "text");
            }
            if($("#status").val()=="1"){
                $('#btnNotice').show();
            }
            // 2 店长
            if($("#status").val()=="2"){
                $('#btnAudit').show();
            }

        }
    });

    //返回按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/huopei'
    });

    // 导出excel
    $('#btnExp').click(function () {
        var shopCode = Feng.getUrlParam("shopCode");
        var beginTime = Feng.getUrlParam("beginTime");
        if (shopCode == "" || shopCode == undefined || shopCode == null) {
            Feng.error("请先选择门店");
        } else {
            //获取导出Excel的数据
            var url = Feng.ctxPath + "/huopei/listExport?shopCode=" + shopCode + "&beginTime=" + beginTime;
            location.href = url;
        }
    });

    // 审核
    function shenHe(date, reason,shopCode,beginTime){
        var shopCode = queryData['shopCode'];
        var beginTime = queryData['beginTime'];
        var ajax = new $ax(Feng.ctxPath + "/huopei/audit", function (data) {

            if (data == 0) {
                Feng.success("推送成功！");
                return;
            }
            if (data == 1) {
                Feng.error("推送失败,请联系人事绑定微信！");
                return;
            }
            if (data == 2) {
                Feng.error("推送失败,请联系店长绑定微信！");
                return;
            }
            if (data == 3) {
                Feng.error("推送失败,请联系财务绑定微信！");
                return;
            }

            Feng.success("推送成功!");
            table.reload(phTable.tableId);
        }, function (data) {

            Feng.error("推送失败!" + data.responseJSON.message + "!");
        });
        ajax.set("shopCode", shopCode);
        ajax.set("beginTime", beginTime);
        ajax.set("status", date);
        ajax.start();
    }
    $('#btnAudit').click(function () {
        layer.open({
            area: ['300px', '250px'],
            title: '审核',
            btn: ['通过', '驳回'],
            btnAlign: 'c',
            type: 1,
            async: false,
            content: ' <div style="text-align:center;" id="div">\n' +
                ' <span class="span" style="line-height: 150px;">确认无误，是否通过？</span>\n' +
                ' <textarea type="text" hidden class="input" id="reason" placeholder="请填写驳回理由"  value="无" style="height: 100px;width: 92%;margin-top: 10px;padding-left: 5px;"></textarea>\n' +
                ' </div>',
            yes: function (index, layero) {
                shenHe("1",queryData['shopCode']);
                layer.close(index);
            },
            btn2: function (index, layero,) {
                $('.input').show();
                $('.span').hide();
                var reason = $('#reason').val();
                if (reason == '') {
                    $('.layui-layer-btn .layui-layer-btn0').hide();
                    document.getElementById("reason").value="无";
                    $('.layui-layer-btn1').text('提交');
                    $('.layui-layer-btn1').css('background', '#1E9FFF');
                    $('.layui-layer-btn1').css('color', '#FFF');
                    return false
                }else {
                    shenHe("0", reason,queryData['shopCode']);
                    layer.close(index);
                }
            },
        });
    });


    // 通知店长
    $('#btnNotice').click(function () {
        var shopCode = queryData['shopCode'];
        var beginTime = queryData['beginTime'];
        var ajax = new $ax(Feng.ctxPath + "/huopei/remind", function (data) {
            if (data == 0) {
                Feng.error("推送失败，请联系店长绑定微信！");
                return;
            }
            Feng.success("推送成功!");
            table.reload(phTable.tableId);
        }, function (data) {
            Feng.error("推送失败!" + data.responseJSON.message + "!");
        });
        ajax.set("shopCode", shopCode);
        ajax.set("beginTime", beginTime);
        ajax.start();

    });


   //监听单元格编辑
    table.on('edit(phTable)', function(obj){
        //得到修改后的值
        var value = obj.value //得到修改后的值
            ,data = obj.data //得到所在行所有键值
            ,field = obj.field; //得到字段

        $.ajax({
                type: "PUT",
                url: "/huopei/editHuopei",
                datatype: "json",
                data: data,
                success: function (data) {
                    table.reload(phTable.tableId);
                }, error: function () {
                    // Feng.error("修改失败！");
                }, complete: function (XHR, TS) { }
            });
    });

    $(".myclass").each(function(){
            $(".input",this).change(function(){
                var shopCode = queryData['shopCode'];
                var beginTime = queryData['beginTime'];
                var turnover = $('#turnover').val();
                var five = $('#five').val();
                var ten = $('#ten').val();
                $.ajax({
                    type: "PUT",
                    url: "/huopei/editHuopei",
                    datatype: "json",
                    data: {'shopCode':shopCode,'ten':ten,'five':five,'turnover':turnover,'beginTime':beginTime} ,
                    success: function (data) {
                        $(".table").load(location.href+" .table");
                    }, error: function () {
                        // Feng.error("修改失败！");
                    }, complete: function (XHR, TS) { }
                });
            });

    });
});
